using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using FireStarter.Base;
using FireStarterModeller.BO;

namespace FireStarter.CodeGeneration
{
/*    /// <summary>
    /// This follows the composite pattern and as such is composed of various generators that
    ///    are used in combination to generate the code from the various templates that are
    ///    sub templates of this class.
    /// The composition of all the tags and templates for this class is constructed (could build using the
    ///     builder pattern).
    /// </summary>
    public class ClassGeneratorTestUtils : ClassGenerator
    {
        private readonly string _boProjectName;
        private readonly IList<IModelClass> _modelElements;
        private string _fileContents = "";
        private IDictionary<string, TagReplacement> _tagReplacements = new Dictionary<string, TagReplacement>(5);

        public ClassGeneratorTestUtils(string className, string projectName, string projectPath, string boProjectName, IList<IModelClass> modelElements)
            : base(className, projectName, projectPath)
        {
            _boProjectName = boProjectName;
            _modelElements = modelElements;
        }

        public string BoProjectName
        {
            get { return _boProjectName; }
        }

        public string FileContents
        {
            get { return _fileContents; }
        }

        public IDictionary<string, TagReplacement> TagReplacements
        {
            get { return _tagReplacements; }
        }

        public override void GenerateCode()
        {
            throw new NotImplementedException();
        }

        public override string GetValueReplacementContent(string tagContent)
        {
            switch (tagContent.ToUpper())
            {
                case "BOPROJECTNAME":
                    return BoProjectName;
                case "PROJECTNAME":
                    return ProjectName;
            }
            return "My valid replacementValue";
        }

        public override IList<IModelClass> GetModelElementsCollection(string tagContent)
        {
            return _modelElements;
        }

        /// <summary>
        /// Reads the template constructs all the dependent tags and based on these tags constructs any dependent
        ///    Templates, load their generation strategy and call its construct method.
        ///    I.e. This load and constructs all the children templates into a hierachical tree.
        /// </summary>
        public override void Construct()
        {
            string templateContents = Templates.Test_BO_TestUtils;
            TemplateParser templateParser = new TemplateParser(this);
            _tagReplacements = templateParser.ExtractAllTagReplacements(templateContents);
            TagReplacement tagReplacement = _tagReplacements["<<FOREACH(BusinessObjects):Test.BO.TestUtils.Generated.CreateBO.txt>>"];

            ForEachReplacement forEachReplacement = (ForEachReplacement) tagReplacement.Replacement;
            FileReplacement fileReplacement = new FileReplacement("SomeTemplate");
            forEachReplacement.Replacement = fileReplacement;
            ValueReplacement_old boClassNameValueReplacement_old = new ValueReplacement_old("<<BOClassName>>");
            boClassNameValueReplacement_old.PropName = "BOClassName";
            fileReplacement.Replacements.Add("<<BOClassName>>", boClassNameValueReplacement_old);
            SimpleReplacement simpleReplacement = new SimpleReplacement("SetDefaultBoPropValue");
            ValueReplacement_old propNameReplacement_old = new ValueReplacement_old("<<PropName>>");
            propNameReplacement_old.PropName = "PropName";
            simpleReplacement.Replacements.Add("<<PropName>>", propNameReplacement_old);
            ForEachReplacement propsForeachReplacement = new ForEachReplacement("<<FOREACH(Props):SetDefaultBoPropValue>>", simpleReplacement);
            fileReplacement.Replacements.Add("<<FOREACH(Props):SetDefaultBoPropValue>>", propsForeachReplacement);
//            foreach (TagReplacement tagReplacement in _tagReplacements.Values)
//            {
//                tagReplacement.
//
//                ForEachReplacement forEachReplacement = tagReplacement.Replacement as ForEachReplacement;
//                if (forEachReplacement != null)
//                {
//                    foreach (IModelClass dmClass in forEachReplacement.ModelElementsCollection)
//                    {
////                        string fileName = TemplateParser.ExtractForTemplateName(tagReplacement.Tag);
//                        TagReplacement childTagReplacement = new TagReplacement("Some text", new FileReplacement("fdaf"));
//                        forEachReplacement.TagReplacements.Add(childTagReplacement);
//                    }
//                }
//            }
        }

        /// <summary>
        /// Compile parses through all the constructed TagReplacements and Replaces the tag with the
        ///    content of its associated parsed template. 
        ///    I.e. this is done hierachically all the children are compiled first
        ///    and the then their parents are compiled untill this object is finally compiled.
        /// </summary>
        public override void Compile()
        {
//            _fileContents = Templates.Test_BO_TestBase;
//            TemplateLoader.SetNamespaceTag(ref _fileContents, ProjectName);
        }
    }*/



    public class TemplateTagException : Exception
    {
        public TemplateTagException()
        {
        }

        public TemplateTagException(string message) : base(message)
        {
        }

        public TemplateTagException(string message, Exception innerException) : base(message, innerException)
        {
        }

        protected TemplateTagException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }
    }
}